package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.service.ClientService;
import com.leyou.common.exception.LyException;
import com.leyou.auth.mapper.ClientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author BaiZY on 2020/9/4.
 * @version 1.0
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientInfo> implements ClientService {

    @Value("${ly.jwt.key}")
    private String key;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public String getSecretKey(String clientId, String secret) {
        //1.查询client的信息
        ClientInfo client = query().eq("client_id", clientId).one();

        if (client == null){
            throw new LyException(401,"客户端信息有误," + clientId + "不存在");
        }

        if (!passwordEncoder.matches(secret, client.getSecret())){
            throw new LyException(401,"客户端信息有误," + secret + "错误");
        }

        //返回秘钥
        return key;
    }
}
